package com.seirsoft.cookingtime.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import com.seirsoft.cookingtime.model.TimeElement;
import com.seirsoft.framework.util.Common;
import com.seirsoft.framework.util.UnicodeString;

public class TimeConfig {
	private static TimeConfig objTimeConfig = null;

	private TimeConfig()
	{
		
	}
	
	public static synchronized TimeConfig getInstance()
	{
		if(objTimeConfig == null)
		{
			objTimeConfig = new TimeConfig();
		}
		
		return objTimeConfig;
	}
	
	public List<TimeElement> getTimeElementList()
	{
		List<TimeElement> aryTimeElement = new ArrayList<TimeElement>();
		
		Properties properties = new Properties();
		
		File file = null;
		FileInputStream fin = null;
		try {
			file = new File("./timeConfig.properties");
			if(file.exists() && file.canRead())
			{
				fin = new FileInputStream(file);//$NON-NLS-1$
				InputStreamReader reader = new InputStreamReader(fin);
				properties.load(reader);
				
				Enumeration<Object> enumeration = properties.keys();
				while(enumeration.hasMoreElements())
				{
					String key = (String) enumeration.nextElement();
					String value = properties.getProperty(key);
					TimeElement objTimeElement = new TimeElement();
					objTimeElement.setStrName(key);
					
					if(Common.isDigital(value))
					{
						objTimeElement.setISecond(Integer.parseInt(value));
					}
					else
					{
						objTimeElement.setISecond(0);
					}
					
					aryTimeElement.add(objTimeElement);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			if(fin != null)
			{
				try {
					fin.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return aryTimeElement;
	}
	
	
	
	public void save(List<TimeElement> aryTimeElement)
	{
		File file = null;
		FileOutputStream fos = null;
		try {
			file = new File("./timeConfig.properties");
			if(! file.exists())
			{
				file.createNewFile();
			}
			
			if(file.exists() && file.canWrite())
			{
				fos = new FileOutputStream(file);//$NON-NLS-1$
				OutputStreamWriter writer = new OutputStreamWriter(fos);
				
				for(int i=0; i<aryTimeElement.size(); i++)
				{
					TimeElement objTimeElement = aryTimeElement.get(i);
					String strName = objTimeElement.getStrName();
					int iSecond = objTimeElement.getISecond();
					
					String key = UnicodeString.getUnicodeString(strName);
					String value = String.valueOf(iSecond);
					
					writer.write(key);
					writer.write('=');
					writer.write(value);
					writer.write('\n');
				}
				
				writer.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			if(fos != null)
			{
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
 
	}
}
